﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Kira.Compiler.Bods
{
    [XmlElementMapping("DITransformCall", "name", "Merge")]
    public class DIMerge : BodsTransform
    {
        public String OutputName
        {
            get
            {
                return this.GetElement("DISchema").GetAttrbuteValue("name");
            }
        }

        public override IEnumerable<string> InputNames
        {
            get { return from c in this.GetElements("DIInputView") select c.GetAttrbuteValue("name"); }
        }

        public override IEnumerable<string> OutputNames
        {
            get { return new String[] { this.OutputName }; }
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(GetComments("Merge"));
            sb.Append("WITH ").Append(OutputName).AppendLine(" AS (");
            bool isFirst = true;
            foreach (var i in InputNames)
            {
                if (isFirst)
                {
                    isFirst = false;
                }
                else
                {
                    sb.AppendLine("UNION ALL");
                }
                sb.Append("SELECT * FROM ").AppendLine(i);
            }
            sb.AppendLine(");");
            return sb.ToString();
        }
    }
}
